Use lazy imports on Python 3.15 to improve startup speed#9733
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
|
I think the import should also belong in |
|
It's certainly a bigger and unrelated change, but looks like |
|
I made the olefile import eager again: it's an optional dependency and lazy imports affect how its related plugins get registered. I'll have a look at logging. Unfortunately it's not straightforward to
|
|
@hugovk That'd work -- but I'm also thinking... do we need the 4 |





Re: #9330
Defer
The first commit here defers the import of
tempfilein four files to the place where it's actually used, so we don't need to pay the import time if we're not using it. This helps for all Python versions.So for example, only importing
PIL.Imageon Python 3.14:Lazy
Python 3.15 introduces lazy imports:
If you put the new
lazykeyword before an import, it won't be actually imported until first use. Butlazyis only for 3.15+, it's a syntax error in 3.10-3.14.So the second commit introduces
__lazy_modules__.We can only put top-level imports here, and not those from function or in
try/except. And we don't need to put any that used right away at the top-level, likelogginginImage.py, but it's not a big deal if we do.Repeating with 3.15: